$(document).ready(function(){
  // 在这里写你的代码...
	
	var contextPath = Com_Parameter.ContextPath;
	//分页函数
	var pagination =function(url,pageNo,pageSize,supplierName,contractNo,subject){
		$.ajax({
			type: "GET",
			url:  url+"?pageSize="+pageSize+"&pageNo="+pageNo,
			data: {"supplierName":supplierName,"contractNo":contractNo,"subject":subject},
			success: function(data) {
				var contractObj = eval('(' + data + ')');
				var contractList = contractObj.jsonRoot;
				$("#intAssociatedTbody").find("tr").each(function(){
					$(this).remove();
				});
				var tbody =$("#intAssociatedTbody");
				for(var i=0;i<contractList.length;i++){
					var contract = contractList[i];
					var tr = "<tr data-herf=\"${contextPath}/biz/contract/associated/"+contract.id+"/view\" data-target=\"_self\">";
						tr += "<td class=\"o-l-tab-ckb\" >";
						tr += "	<input type=\"checkbox\" data-ckb-id=\""+contract.id+"\" title=\""+i+"\" >";
						tr += "	</td>";
						tr +=	"<td>"+contract.contractNo+"</td>";
						tr +=	"<td>"+contract.subject+"</td>";
						tr +=	"<td >"+contract.supplierName+"</td>";
						tr += "</tr>";
						tbody.append(tr);
				}
				var total =parseFloat(contractObj.total);
				var totalSize = Math.ceil(total/parseFloat(pageSize));
				$("#pageNo").val(contractObj.pageNo);
				$("#pageSize").val(contractObj.pageSize);
				$("#total").val(contractObj.total);
				$("#pageNoSpan").text("第"+contractObj.pageNo+"页");
				$("#totalSizeSpan").text("共"+totalSize+"页");
				if(contractObj.pageNo ==1){
					$("#lastPageLi").attr("class","disabled");
				}else if(contractObj.pageNo >=totalSize){
					$("#nextPageLi").attr("class","disabled");
				}else{
					$("#lastPageLi").attr("class","");
					$("#nextPageLi").attr("class","");
				}
			}
		});
	}
	/***********************************ajax分页查询合同信息******************************/
	//条件查询
	$("#queryContract").click(function(){
		var supplierName = $("#suppliername").val();
		var contractNo = $("#contractno").val();
		var subject =$("#subject").val();
		var pageNo = 1;
		var pageSize =  parseFloat($("#pageSize").val());
		var url = contextPath+"/biz/intent/common/getModuleContractListPage"
		pagination(url,pageNo,pageSize,supplierName,contractNo,subject);
	});
	
	//跳转页面输入框只能输入正整数
	$("#gotoPageNo").keyup(function(){
		var str=$(this).val();
		var newstr='';
		 for(i=0;i<str.length;i++){
			 var j=str.charCodeAt(i);
			 if(j>47&&j<55){
				 newstr+=String.fromCharCode(j); 
			 }
		  }
		 if( newstr.charCodeAt(0)==48 ){
			 $(this).val(newstr.substr(1,newstr.length)); 
		 } else{ 
			 $(this).val(newstr); 
		}
	});
	
	//跳转到指定页面
	$("#goto").click(function(){
		var contractNo = $("#contractno").val();
		var supplierName = $("#suppliername").val();
		var subject =$("#subject").val();
		var pageNo = $("#gotoPageNo").val();
		var pageSize = parseFloat($("#pageSize").val());
		var total =parseFloat($("#total").val());
		var totalSize = Math.ceil(total/pageSize);
		var url = contextPath+"/biz/intent/common/getModuleContractListPage"
		if(pageNo ==null && ''==pageNo){
			top.$.jBox.tip('请输入页码','warning');
			return;
		}else if(pageNo >totalSize ||pageNo<1){
			top.$.jBox.tip('你输入的页码不在范围内','warning');
			return;
		}
		pagination(url,pageNo,pageSize,supplierName,contractNo,subject);
	});
	
	//上一页
	$("#lastPage").click(function(){
		var contractNo = $("#contractno").val();
		var supplierName = $("#suppliername").val();
		var subject =$("#subject").val();
		var pageNo = parseFloat($("#pageNo").val());
		var pageSize = parseFloat($("#pageSize").val());
		var total =parseFloat($("#total").val());
		var nextPageNo = pageNo + 1;
		var lastPageNo = pageNo -1;
		var totalSize = Math.ceil(total/pageSize);
		var url = contextPath+"/biz/intent/common/getModuleContractListPage"
		if(pageNo <=1){
			top.$.jBox.tip('没有上一页','warning');
			return;
		}
		pagination(url,lastPageNo,pageSize,supplierName,contractNo,subject);
	});
	//下一页
	$("#nextPage").click(function(){
		var contractNo = $("#contractno").val();
		var supplierName = $("#suppliername").val();
		var subject =$("#subject").val();
		var pageNo = parseFloat($("#pageNo").val());
		var pageSize = parseFloat($("#pageSize").val());
		var total =parseFloat($("#total").val());
		var nextPageNo = pageNo + 1;
		var totalSize = Math.ceil(total/pageSize);
		var url = contextPath+"/biz/intent/common/getModuleContractListPage"
		if(pageNo >=totalSize){
			top.$.jBox.tip('没有下一页','warning');
			return;
		}
		pagination(url,nextPageNo,pageSize,supplierName,contractNo,subject);
	});
	
});